package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 工作台今日数据查询
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO getBusinessDate(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */

        Map map = new HashMap<>();
        map.put("begin" ,begin);
        map.put("end" ,end);

        //统计总订单
        Integer validOrderSum = orderMapper.countByMap(map);

        map.put("status" ,5);
        //1.营业额统计
        Double turnover = orderMapper.sumBYMap(map);
        //2.统计有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        Double orderCompletionRate = 0.0;
        //计算订单完成率
        if(validOrderSum != 0){
            orderCompletionRate = validOrderCount * 1.0 / validOrderSum;
        }

        Double unitPrice = 0.0;
        //计算平均客单价
        if(validOrderCount != 0){
            unitPrice = turnover / validOrderCount;
        }

        //计算新增用户数
        Integer newUser = userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUser)
                .build();
    }

    /**
     * 工作台订单管理
     * @param begin
     * @param end
     * @return
     */
    public OrderOverViewVO getOverviewOrders(LocalDateTime begin, LocalDateTime end) {

        /**
         * 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
         */

        //1.统计全部订单
        Map map = new HashMap<>();
        map.put("begin" ,begin);
        map.put("end" ,end);

        Integer allOrders = orderMapper.countByMap(map);

        //2.统计待接单数量
        map.put("status" ,2);
        Integer waitingOrders = orderMapper.countByMap(map);

        //3.统计待派送数量(相当于已接单)
        map.put("status" ,3);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //4.统计已完成数量
        map.put("status" ,5);
        Integer completedOrders = orderMapper.countByMap(map);

        //5.统计已取消数量
        map.put("status" ,6);
        Integer cancelledOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 工作台菜品总览接口
     * @return
     */
    public DishOverViewVO getOverviewDishes() {
        //1.统计菜品已起售数量
        Integer sold = dishMapper.getByStatus(StatusConstant.ENABLE);
        //2.已停售数量
        Integer discontinued = dishMapper.getByStatus(StatusConstant.DISABLE);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 工作台查询套餐总览
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
        //1.统计已起售数量
        Integer sold = setmealMapper.getByStatus(StatusConstant.ENABLE);
        //2.统计已停售数量
        Integer discontinued = setmealMapper.getByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
